package ca.ubc.cpsc210.waldo.util;

/**
 * A latitude/longitude coordinate pair
 */
public class LatLon {
	private double lat;
	private double lon;

	public LatLon(String lat, String lon) {
		this.lat = Double.parseDouble(lat);
		this.lon = Double.parseDouble(lon);
	}

	public LatLon(double lat, double lon) {
		this.lat = lat;
		this.lon = lon;
	}

	public double getLatitude() {
		return lat;
	}

	public double getLongitude() {
		return lon;
	}
	
	public boolean isIllegal() {
		if (lat < 0 || lat > 90 || lon < -180 || lon > 180)
			return true;
		return false;
	}

	@Override
	public String toString() {
		return lat + ", " + lon;
	}
	
	@Override
	public boolean equals(Object other) {
		LatLon otherAsLatLon;
		if (other instanceof LatLon) {
			otherAsLatLon = (LatLon) other;
			if (otherAsLatLon.lat == lat && otherAsLatLon.lon == lon)
				return true;
		}
		return false;
	}

	public static double distanceBetweenTwoLatLon(LatLon point1, LatLon point2) {
		double d2r = Math.PI / 180;

		double dlong = (point2.getLongitude() - point1.getLongitude()) * d2r;
		double dlat = (point2.getLatitude() - point1.getLatitude()) * d2r;
		double a = Math.pow(Math.sin(dlat / 2.0), 2)
				+ Math.cos(point1.getLatitude() * d2r)
				* Math.cos(point2.getLatitude() * d2r)
				* Math.pow(Math.sin(dlong / 2.0), 2);
		double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
		double d = 6367 * c;
		d = d * 1000;
		// System.out.println("LatLon Dist:" + d);
		return d;
	}
}
